import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)
const title = Vue.directive('title', {
  inserted: function (el) {
    document.title = el.getAttribute('title');
  }
})
export default new Vuex.Store({
  state: {
    router: JSON.parse(localStorage.getItem('routerList')) || [],
    list: [],

  },
  getters: {
  },
  mutations: {
    getInfo(state) {
      axios({
        url: 'http://43.143.166.235:8090/chaxun/info',
        headers: {
          'X-Litemall-Token': localStorage.getItem("e_token")
          // 'X-Litemall-Token': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIGxpdGVtYWxsIHRva2VuIiwiYXVkIjoiTUlOSUFQUCIsImlzcyI6IkxJVEVNQUxMIiwiZXhwIjoxNzYyMDk2NzUzLCJ1c2VySWQiOjYsImlhdCI6MTY5ODkzODM1M30.sB-04xhSn_4Bjae_MT2nr0u0bvQGNLV11vf9hEVNd6A"
        }
      }).then(res => {

        console.log('当前路由角色', res)
        state.list = res.data.data
        localStorage.setItem('user', JSON.stringify(res.data.data))
        if (state.list.name === 'admian') {
          axios({
            url: "http://43.143.166.235:8090/select/adminrouter",
          }).then(res => {
            console.log('全局的INFO', res)
            // this.router = res.data.list
            state.router = res.data.list
            localStorage.setItem('routerList', JSON.stringify(res.data.list))

          })
        } else {
          axios({
            url: "http://43.143.166.235:8090/select/userrouter"
          }).then(res => {
            console.log("普通用户", res)
            state.router = res.data
            localStorage.setItem('routerList', JSON.stringify(res.data))
          })
        }
      })
    }
  },
  actions: {
  },
  modules: {
  }
})
